<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./js/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <h1 :style="{opacity:opacity}">Vue真不错</h1>
      <button @click="$destroy()">销毁当前实例</button>
    </div>
  </body>
</html>
<script>
  Vue.config.productionTip = false;
  const vm = new Vue({
    el: '#app',
    data() {
      return {
        opacity: 1,
        timer: null,
      };
    },

    // mounted生命周期函数，当vue挂载完成后执行，生命周期其实也是vue的配置项
    mounted() {
      this.timer = setInterval(() => {
        console.log(1);
        this.opacity -= 0.01;
        if (this.opacity <= 0) {
          this.opacity = 1;
        }
      }, 16);
    },

    // 实例销毁也有一个生命周期函数，beforeDestroy
    beforeDestroy() {
      clearInterval(this.timer);
    },
  });
</script>
